import { webToPdf, convertToReadingMode, pure } from "./lib/puppeteer.js";

import { glob } from "glob";

import fsPromises from "fs/promises";

let files = await glob(`./data/*.mhtml`);

(async () => {
  // await main();

  const file = files[96 - 1];
  const url = await getFileUrl(file);
  await webToPdf(url, {
    fileName: "96.pdf",
  });

  // await pure("https://www.cnblogs.com/bmjoker/p/9084394.html");
  // await webToPdf("https://www.cnblogs.com/bmjoker/p/10051014.html", {
  //   fileName: "test.pdf",
  // });
  // await convertToReadingMode(
  //   "https://www.cnblogs.com/bmjoker/p/9084394.html"
  // );
})();

async function main() {
  const index = Number(await fsPromises.readFile("./config.txt", "utf-8")) || 0;

  console.log("初始化index", index);

  for (let i = index; i < files.length; i++) {
    console.log("当前执行", i);
    await fsPromises.writeFile("./config.txt", i.toString(), "utf-8");

    const file = files[i];
    const fileName =
      `${i + 1}-` +
      file.split(" - bmjoker - 博客园.mhtml")[0].replace("data\\", "") +
      ".pdf";
    const url = await getFileUrl(file);

    console.log("fileName", fileName);

    await webToPdf(url, {
      dir: "output",
      fileName,
    });

    await new Promise((resolve) => setTimeout(resolve, 1000 * 5));
  }
}

function extractContentLocationUrl(text) {
  // 使用正则表达式匹配 Content-Location: 后面的URL
  const regex = /Content-Location:\s*(https?:\/\/[^\s]+)/i;
  const match = text.match(regex);

  // 如果找到匹配项，返回URL，否则返回null
  return match ? match[1] : null;
}

async function getFileUrl(file) {
  return await fsPromises.readFile(file, "utf-8").then((text) => {
    const url = extractContentLocationUrl(text);
    return url;
  });
}
